package demo.xlj.yuyi.studentapp.register;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.IdRes;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.RadioGroup;

import butterknife.BindView;
import butterknife.ButterKnife;
import demo.xlj.yuyi.studentapp.R;
import demo.xlj.yuyi.studentapp.base.BaseRxActivity;
import demo.xlj.yuyi.studentapp.bean.RegisterBean;
import demo.xlj.yuyi.studentapp.retrofit.RetrofitClient;
import demo.xlj.yuyi.studentapp.rxjava.RxSchedulers;
import demo.xlj.yuyi.studentapp.utils.ToastUtil;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import top.wefor.circularanim.CircularAnim;

/**
 * A login screen that offers login via email/password.
 */
public class StudentMeetRegisterActivity extends BaseRxActivity {


    @BindView(R.id.editText_student_meet_register_zhanghao)
    EditText mEditTextStudentMeetRegisterZhanghao;
    @BindView(R.id.editText_student_meet_register_mima)
    EditText mEditTextStudentMeetRegisterMima;
    @BindView(R.id.editText_student_meet_register_mima_2)
    EditText mEditTextStudentMeetRegisterMima2;
    @BindView(R.id.editText_student_meet_register_jiaosha)
    EditText mEditTextStudentMeetRegisterJiaosha;
    @BindView(R.id.radioGroup_student_meet_register)
    RadioGroup mRadioGroupStudentMeetRegister;
    @BindView(R.id.progressBar_student_meet_register)
    ProgressBar mProgressBarStudentMeetRegister;
    @BindView(R.id.button_student_meet_register_sign_in_button)
    Button mButtonStudentMeetRegisterSignInButton;


    private String zhanghao;
    private String mima;
    private String mima_2;
    private String mingzi;
    private int userInfo = 99;

//    private UserDao mUserDao;

    private String otherZhanghao;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_student_meet_register2);
        ButterKnife.bind(this);
        initListener();
//        mUserDao = BaseApplication.instances.getDaoSession().getUserDao();

    }

    /**
     * 完成初始化一系列的监听
     */
    private void initListener() {
        mRadioGroupStudentMeetRegister.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case R.id.radioButton_student_meet_register_one:
                        userInfo = 0;
                        break;

                    case R.id.radioButton_student_meet_register_two:
                        userInfo = 1;

                        break;

                    case R.id.radioButton_student_meet_register_three:
                        userInfo = 2;

                        break;
                    case R.id.radioButton_student_meet_register_four:
                        userInfo = 3;

                        break;

                    default:
                        userInfo = 88;

                        break;
                }
            }
        });
    }


    /**
     * 用户注册账号按钮
     *
     * @param view
     */
    public void btn_student_meet_register_go(View view) {
        zhanghao = mEditTextStudentMeetRegisterZhanghao.getText().toString();
        mima = mEditTextStudentMeetRegisterMima.getText().toString();
        mima_2 = mEditTextStudentMeetRegisterMima2.getText().toString();
        mingzi = mEditTextStudentMeetRegisterJiaosha.getText().toString();


        if (TextUtils.isEmpty(zhanghao)) {
            mEditTextStudentMeetRegisterZhanghao.setError("账号不能为空");
            return;
        }

        if (TextUtils.isEmpty(mima)) {
            mEditTextStudentMeetRegisterMima.setError("密码不能为空");
            return;
        }

        if (TextUtils.isEmpty(mima_2)) {
            mEditTextStudentMeetRegisterMima2.setError("请再输入一遍密码");
            return;
        }

        if (TextUtils.isEmpty(mingzi)) {
            mEditTextStudentMeetRegisterJiaosha.setError("请输入姓名");
            return;
        }

        if (userInfo == 99) {
            ToastUtil.info(StudentMeetRegisterActivity.this, "请选择您的身份", true, true);
            return;
        }

        if (userInfo == 88) {
            ToastUtil.error(StudentMeetRegisterActivity.this, "暂时不提供注册功能，请稍后重试", true, true);
            return;
        }

        if (!mima.equals(mima_2)) {
            ToastUtil.error(StudentMeetRegisterActivity.this, "请输入一样的密码", true, true);
            return;
        }

        if (zhanghao.equals(otherZhanghao)) {
            ToastUtil.error(StudentMeetRegisterActivity.this, "不能注册名字一样的账号", true, true);
            return;
        }

        otherZhanghao = zhanghao;

        CircularAnim.hide(mButtonStudentMeetRegisterSignInButton).go();

        myHandler.sendEmptyMessageDelayed(11, 618);

    }

    private Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 11:
                    mEditTextStudentMeetRegisterZhanghao.setText("");
                    mEditTextStudentMeetRegisterMima2.setText("");
                    mEditTextStudentMeetRegisterMima.setText("");
                    mEditTextStudentMeetRegisterJiaosha.setText("");
                    mProgressBarStudentMeetRegister.setVisibility(View.VISIBLE);
                    //联网操作
                    addRxDestroy(RetrofitClient.getInstance(StudentMeetRegisterActivity.this)
                            .getApiService()
                            .addUser(zhanghao, mima, mingzi, userInfo + "")
                            .compose(RxSchedulers.<RegisterBean>io_main())
                            .subscribe(new Consumer<RegisterBean>() {
                                @Override
                                public void accept(@NonNull RegisterBean registerBean) throws Exception {
                                    if (registerBean.getStatus() == 10006) {
                                        mProgressBarStudentMeetRegister.setVisibility(View.INVISIBLE);
                                        ToastUtil.success(StudentMeetRegisterActivity.this, "恭喜您注册成功,请返回登录界面登录", true, true);
                                    } else {
                                        ToastUtil.info(StudentMeetRegisterActivity.this, "注册失败，请换个名字试试哦", true, true);
                                    }
                                    myHandler.sendEmptyMessageDelayed(33, 500);
                                }
                            }, new Consumer<Throwable>() {
                                @Override
                                public void accept(@NonNull Throwable throwable) throws Exception {
                                    ToastUtil.error(StudentMeetRegisterActivity.this, "请检查网络设置哦", true, true);
                                    myHandler.sendEmptyMessageDelayed(33, 500);
                                }
                            }));
//                    //操作数据库
//                    User user = new User(null, zhanghao, mima, userInfo, mingzi);
//                    mUserDao.insert(user);

                    break;

                case 33:
                    CircularAnim.show(mButtonStudentMeetRegisterSignInButton).go();
                    break;
            }
        }
    };

}

